﻿using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;

namespace DomainComponents.Common {
    [DomainComponent]
    [NonPersistentDc]
    public interface IAddressable {
        [DevExpress.ExpressApp.DC.Aggregated]
        [VisibleInListView(false)]
        IBaseAddress PrimaryAddress { get; set; }

        [CreateInstance]
        IBaseAddress CreateAddress();

        void Copy(IAddressable target);
    }
    [DomainLogic(typeof(IAddressable))]
    public class AddressableLogic {
        public static void AfterConstruction(IAddressable addressable) {
            if(addressable.PrimaryAddress == null) {
                addressable.PrimaryAddress = addressable.CreateAddress();
            }
        }
        public static void Copy(IAddressable source, IAddressable target) {
            if(target != null && source.PrimaryAddress != null && target.PrimaryAddress != null) {
                source.PrimaryAddress.CopyTo(target.PrimaryAddress);
            }
        }
    }

    [DomainComponent]
    [XafDefaultProperty("DisplayName")]
    public interface IBaseAddress {
        //[PersistentDc]
        string DisplayName { get; set; }
        void CopyTo(IBaseAddress targetAddress);
    }

    [DomainLogic(typeof(IBaseAddress))]
    public class BaseAddressLogic {
        public static void CopyTo(IBaseAddress address, IBaseAddress targetAddress) {
            targetAddress.DisplayName = address.DisplayName;
        }
    }
    
    [DomainComponent]
    public interface IGenericAddress : IBaseAddress {
        string Street1 { get; set; }
        string Street2 { get; set; }
        string City { get; set; }
        string State { get; set; }
        string Zip { get; set; }
        string Country { get; set; }
    }

    [DomainLogic(typeof(IGenericAddress))]
    public class GenericAddressLogic {
        private static void UpdateDisplayName(IGenericAddress address) {
            address.DisplayName = string.Format("{0} {1} {2} {3} {4} {5}", address.Street2, address.Street1, address.City, address.State,
                address.Zip, address.Country);
        }
        public static void CopyTo(IBaseAddress address, IBaseAddress targetAddress) {
            IGenericAddress source = address as IGenericAddress;
            IGenericAddress target = targetAddress as IGenericAddress;
            if(source != null && target != null) {
                target.Street1 = source.Street1;
                target.Street2 = source.Street2;
                target.City = source.City;
                target.State = source.State;
                target.Zip = source.Zip;
                target.Country = source.Country;
            }
        }
        public static void AfterChange_Street1(IGenericAddress address) {
            UpdateDisplayName(address);
        }
        public static void AfterChange_Street2(IGenericAddress address) {
            UpdateDisplayName(address);
        }
        public static void AfterChange_City(IGenericAddress address) {
            UpdateDisplayName(address);
        }
        public static void AfterChange_State(IGenericAddress address) {
            UpdateDisplayName(address);
        }
        public static void AfterChange_Zip(IGenericAddress address) {
            UpdateDisplayName(address);
        }
        public static void AfterChange_Country(IGenericAddress address) {
            UpdateDisplayName(address);
        }
    }
}
